local null_ls = require "null-ls"

local b = null_ls.builtins

local sources = {

  -- webdev stuff
  b.formatting.deno_fmt, -- choosed deno for ts/js files cuz its very fast!
  b.formatting.prettier.with { filetypes = { "html", "markdown", "css" } }, -- so prettier works only on these filetypes

  -- Lua
  b.formatting.stylua,

  -- cpp
  b.formatting.clang_format,

  -- python
  b.diagnostics.ruff.with {
    extra_args = {
      "--line-length=150",
      "--ignore=E402",
    },
  },
  b.formatting.ruff.with {
    "--line-length=150",
  },
}

null_ls.setup {
  on_attach = function()
    vim.api.nvim_create_augroup("format_on_save", { clear = true })
    vim.api.nvim_create_autocmd("BufWritePre", {
      group = "format_on_save",
      pattern = "*",
      callback = function()
        -- on 0.8, you should use vim.lsp.buf.format({ bufnr = bufnr }) instead
        -- on later neovim version, you should use vim.lsp.buf.format({ async = false }) instead
        vim.lsp.buf.format()
      end,
    })
  end,
  debug = true,
  sources = sources,
}
